function addFilter() {
	addExistentFilter("", "", "");
}

function addExistentFilter(fieldName, filterMethod, expression) {

	var filtersSection = document.getElementById("filtersSection");
	var filters = filtersSection.childNodes;
	var nextFilter = filters.length;
	var newFilter = document.createElement("div");
	newFilter.id = "filter" + nextFilter;

	if (nextFilter == 0) {
		addSeparator(newFilter);
	}

	addFieldName(fieldName, newFilter, nextFilter);
	addFilterMethod(filterMethod, newFilter, nextFilter);
	addExpression(expression, newFilter, nextFilter);
	addDeleteButton(newFilter, nextFilter);

	addSeparator(newFilter);

	filtersSection.appendChild(newFilter);

}

function addSeparator(newFilter) {
	var p0 = document.createElement("p");

	addTextLine(p0,
			"==========================================================================");

	newFilter.appendChild(p0);
}

function addFieldName(fieldName, newFilter, nextFilter) {

	var p = document.createElement("p");

	addTextLine(p, "Field Name:");

	var select = document.createElement("select");
	select.id = "fieldName" + nextFilter;
	select.name = "fieldName" + nextFilter;

	var optionNames = new Array("Data", "HTTP accept", "HTTP accept encoding",
			"HTTP accept language", "HTTP authbasic", "HTTP authorization",
			"HTTP cache control", "HTTP connection", "HTTP content encoding",
			"HTTP content length", "HTTP content length header",
			"HTTP content type", "HTTP cookie", "HTTP date", "HTTP host",
			"HTTP last modified", "HTTP location", "HTTP notification",
			"HTTP proxy authenticate", "HTTP proxy authorization",
			"HTTP proxy connect host", "HTTP proxy connect port",
			"HTTP referer", "HTTP request", "HTTP request method",
			"HTTP request URI", "HTTP request version", "HTTP response",
			"HTTP response code", "HTTP server", "HTTP set cookie",
			"HTTP transfer encoding", "HTTP user agent",
			"HTTP www authenticate", "HTTP x forwarded for");

	var optionValues = new Array("data", "http.accept", "http.accept_encoding",
			"http.accept_language", "http.authbasic", "http.authorization",
			"http.cache_control", "http.connection", "http.content_encoding",
			"http.content_length", "http.content_length_header",
			"http.content_type", "http.cookie", "http.date", "http.host",
			"http.last_modified", "http.location", "http.notification",
			"http.proxy_authenticate", "http.proxy_authorization",
			"http.proxy_connect_host", "http.proxy_connect_port",
			"http.referer", "http.request", "http.request.method",
			"http.request.uri", "http.request.version", "http.response",
			"http.response.code", "http.server", "http.set_cookie",
			"http.transfer_encoding", "http.user_agent",
			"http.www_authenticate", "http.x_forwarded_for");

	for (i = 0; i < optionNames.length; i++) {
		var selected = optionValues[i] == fieldName;
		addOption(select, optionNames[i], optionValues[i], selected);
	}

	p.appendChild(select);

	newFilter.appendChild(p);
}

function addOption(select, text, value, selected) {

	var option = document.createElement("option");
	option.value = value;

	if (selected == true) {
		option.selected = "selected";
	}

	var text = document.createTextNode(text);
	option.appendChild(text);

	select.appendChild(option);
}

function addFilterMethod(filterMethod, newFilter, nextFilter) {

	var p = document.createElement("p");

	addTextLine(p, "Filter Method:");

	var select = document.createElement("select");
	select.id = "filterMethod" + nextFilter;
	select.name = "filterMethod" + nextFilter;

	var optionNames = new Array("Contains", "Does not Contain", "Equals",
			"Not Equals",

			// TODO : enable these XSLT 2.0 features
			// "Greater Than",
			// "Greater or Equals",
			// "Less Than",
			// "Less or Equals",

			"Starts With", "Does Not Start With"

	// TODO : enable these XSLT 2.0 features
	// ,
	// "Ends With",
	// "Does Not End With",
	// "Matches",
	// "Does not Matche"
	);

	var optionValues = new Array("contains", "not contains", "equals", "not equals",

	// TODO : enable these XSLT 2.0 features
			// ">",
			// ">=",
			// "<",
			// "<=",

			"starts with", "not starts with"

	// TODO : enable these XSLT 2.0 features
	// ,
	// "ends with",
	// "not ends with",
	// "matches",
	// "not matches"
	);

	for (i = 0; i < optionNames.length; i++) {
		var selected = optionValues[i] == filterMethod;
		addOption(select, optionNames[i], optionValues[i], selected);
	}

	p.appendChild(select);

	newFilter.appendChild(p);
}

function addExpression(expression, newFilter, nextFilter) {

	var p = document.createElement("p");

	addTextLine(p, "Expression:");

	var expressionInput = document.createElement("input");
	expressionInput.type = "text";
	expressionInput.id = "expression" + nextFilter;
	expressionInput.name = "expression" + nextFilter;
	expressionInput.size = "40";
	expressionInput.value = expression;

	p.appendChild(expressionInput);

	newFilter.appendChild(p);
}

function deleteFilter(id) {
	var filtersSection = document.getElementById("filtersSection");
	var filterToDelete = document.getElementById("filter" + id);
	filtersSection.removeChild(filterToDelete);
	
	var fieldNameArray = new Array();
	var filterMethodArray = new Array();
	var expressionArray = new Array();

	var arrayIndex = 0;
	var maxIndex = filtersSection.childNodes.length + 1;
	for (var i = id + 1; i < maxIndex; i++) {

		fieldNameArray[arrayIndex] = document.getElementById("fieldName" + i).value;
		filterMethodArray[arrayIndex] = document.getElementById("filterMethod" + i).value;
		expressionArray[arrayIndex] = document.getElementById("expression" + i).value;
		
		var filterToMove = document.getElementById("filter" + i);
		filtersSection.removeChild(filterToMove);
		
		arrayIndex++;
	}
	
	for (var j = 0; j < arrayIndex; j++) {
		addExistentFilter(fieldNameArray[j], filterMethodArray[j], expressionArray[j]);
	}	
}

function addDeleteButton(newFilter, nextFilter) {

	var p = document.createElement("p");

	var deleteFilter = document.createElement("input");
	deleteFilter.type = "button";
	deleteFilter.id = "delete" + nextFilter;
	deleteFilter.name = "delete" + nextFilter;
	deleteFilter.value = "Delete Filter";
	deleteFilter.setAttribute("onclick", "deleteFilter(" + nextFilter + ")");

	p.appendChild(deleteFilter);

	newFilter.appendChild(p);
}

function addTextLine(p, text) {

	var separatorText = document.createTextNode(text);
	p.appendChild(separatorText);

	var br = document.createElement("br");
	p.appendChild(br);
}

function showFileContent(theLocation) {

	var ruleContent = document.getElementById("ruleContent");
	var ruleContentText = document.createTextNode("file content");

	ruleContent.appendChild(ruleContentText);
}

function showNormal() {
	var ruleContent = document.getElementById("ruleContent");
	ruleContent.setAttribute("disabled", "disabled");

	var addFilterButton = document.getElementById("addFilterButton");
	addFilterButton.removeAttribute("disabled");
}

function showCustom() {

	var ruleContent = document.getElementById("ruleContent");
	ruleContent.removeAttribute("disabled");

	var addFilterButton = document.getElementById("addFilterButton");
	addFilterButton.setAttribute("disabled", "disabled");

}
